<?php
require_once("includes/include.php");
$Helper= new Helper();
$Log= new Log();
$post = $_POST;
if (!empty($post)&&$post["postType"]==1){
	$post['name'] = Helper::formDataFilter($post['name']);
	$post['description'] = Helper::formDataFilter($post['description']);
	$post['pwd']= Helper::formDataFilter($post['pwd']);
	@session_start();
	$ownerId = $_SESSION["userId"];
	date_default_timezone_set("Asia/shanghai");
	$createDate =date("Y-m-d H:i:s");
	$dao = new picturesdao();
	$kind="1";//1 stands for pictures folders;
	$create = $dao->createPicFolder($post['name'],$post['description'],$post['quanxian'],$post['pwd'],$ownerId,$kind,$createDate);
	if($create){
		$cover = $create["cover"];
		 	$name  = $create["name"];
		 	$folderId  =  $create["id"];
		 	$number = 0;
		 	if ($cover==""){
		 		$cover = $Helper->getPicturceFolderDefaultCover();
		 	}
		 	echo "<div class='picturefolder' id='folder_$folderId'>";
			echo "<img src='$cover' alt='$name' onclick=\"disPlayPicS('$folderId')\" /><br />";
		 	echo $name."<br />";
		 	echo "共<span id='picturefolderInfoSpan_$folderId'>".$number."张</span>";
		 	echo "</div>";
	}
	else echo "error";
}else if (!empty($post)&&$post["postType"]==2){
	//display pictures in folder;
	$foderId = $post['folderId'];
	$dao = new picturesdao();
	$pictures = $dao->getPicS($foderId);
	if(!empty($pictures)){
		echo "<div id=\"closPicsDisplayFolder\" onclick=\"closPicsDisplayFolder('$foderId')\" style=\"margin-top:-26px;margin-right:-5px;float:right;cursor:pointer;\"><img src=\"style/window_close.png\" /></div>";
		echo "<div id='uploadInfoAdd_$foderId' class='uploadInfoAdd'>";
		echo "<input id='file_upload_$foderId' name='file_upload_$foderId' type='file' />";
		echo "<input type='button' class='uploadButton' id='uploadthis_$foderId' onclick=\"javascript:$('#file_upload_$foderId').uploadifyUpload()\" value='上传' /><br /><br />";
		echo "<div id='deletepicsFolder'><a onclick=\"deletePicFolder('$foderId')\" style='cursor: pointer;' ><img src='style/no.png' />删除相册</a></div>";
		echo "</div>";
		echo "<script>
			createDynamicUpload('$foderId','file_upload_$foderId','uploadInfoAdd_$foderId');";
		//echo "$(\"#uploadthis_$foderId\").click($(\"#file_upload_$foderId\").uploadifyUpload);";
		echo "</script>";
		echo "<div id='picsList_$foderId'>";
		foreach ($pictures as $picture){
			$picsSize = getimagesize($picture['path']);
			$width = $picsSize[0];
			$height = $picsSize[1];
			$max = $width>$height?$width:$height;
			$bei = $max/300;
			$width=$width/$bei;
			$height=$height/$bei;
			if ($height<$width){
				$margin = (300-$height)/2;
			}else{
				$margin=0;
			}
			echo "<div class='singlePic' idhandle=$picture[id]><div class='imgdivwrapper'><img src='".$picture['path']."' alt='".$picture['path']."' width='$width' height='$height' onclick=\"displayTargetPic('$picture[id]','$foderId')\" style='margin-top:".$margin."px;' /></div><br />$picture[name]<br /><a onclick=\"deleteTargetPic('$picture[id]','$foderId')\" style='cursor:pointer;'>删除</a>&nbsp;&nbsp;<a onclick=\"setFolderCover('$picture[id]','$foderId')\" style='cursor:pointer;'>设为封面</a></div>";
		}
		echo "</div>";	
		
	}else{
		echo "<div id=\"closPicsDisplayFolder\" onclick=\"closPicsDisplayFolder('$foderId')\" style=\"margin-top:-26px;margin-right:-5px;float:right;cursor:pointer;\"><img src=\"style/window_close.png\" /></div>";
		echo "还没有照片,赶快上传照片<br /><br />";
		echo "<div id='uploadInfoAdd_$foderId' class='uploadInfoAdd'>";
		echo "<input id='file_upload_$foderId' name='file_upload_$foderId' type='file' />";
		echo "<input type='button' id='uploadthis_$foderId' onclick=\"javascript:$('#file_upload_$foderId').uploadifyUpload()\" value='上传' /><br /><br />";
		echo "<a onclick=\"deletePicFolder('$foderId')\" style='cursor: pointer;' >删除相册</a>";
		echo "<script>
			createDynamicUpload('$foderId','file_upload_$foderId','uploadInfoAdd_$foderId');";
		//echo "$(\"#uploadthis_$foderId\").click($(\"#file_upload_$foderId\").uploadifyUpload);";
		echo "</script>";
		echo "</div>";
	}
}else if(!empty($post)&&$post["postType"]==3){
	$foderId = $post["folderId"];
	$dao = new picturesdao();
	$picturesNumber = $dao->countPictures($foderId);
	echo $picturesNumber;
}else if(!empty($post)&&$post["postType"]==4){
	//add new pics in folder;
	$foderId = $post["folderId"];
	$dao = new picturesdao();
	$picturesNumber = $dao->countPictures($foderId);
	echo $picturesNumber.";";
	
	$pictures = $dao->getPicS($foderId);
	foreach ($pictures as $picture){
		$picsSize = getimagesize($picture['path']);
			$width = $picsSize[0];
			$height = $picsSize[1];
			$max = $width>$height?$width:$height;
			$bei = $max/300;
			$width=$width/$bei;
			$height=$height/$bei;
			if ($height<$width){
				$margin = (300-$height)/2;
			}else{
				$margin=0;
			}
			echo "<div class='singlePic' idhandle=$picture[id]><div class='imgdivwrapper'><img src='".$picture['path']."' alt='".$picture['path']."' width='$width' height='$height' onclick=\"displayTargetPic('$picture[id]','$foderId')\" style='margin-top:".$margin."px;' /></div><br />$picture[name]<br /><a onclick=\"deleteTargetPic('$picture[id]','$foderId')\" style='cursor:pointer;'>删除</a>&nbsp;&nbsp; <a onclick=\"setFolderCover('$picture[id]','$foderId')\" style='cursor:pointer;'>设为封面</a></div>";
		}
	
}else if(!empty($post)&&$post["postType"]==5){
	//display target picture
	$picId = $post['picId'];
	$folderId = $post['folderId'];
	$dao = new picturesdao();
	$targetPicInfo = $dao->getTargetPicInfo($picId,$folderId);
	if (!empty($targetPicInfo)){
		$target = $targetPicInfo['target'];
		$pre = $targetPicInfo['pre'];
		$next = $targetPicInfo['next'];
		$path = $target['path'];
		$picsSize = getimagesize($path);
		$width=$picsSize[0];
		$height = $picsSize[1];
		$marginT =$height/2;
		$closeStr= "<div id='closePicSingleDisplay' onclick=\"closePicSingleDisplay('displaySingleTargPic')\" style='float:right;cursor:pointer;margin-top:-10px;'>
		<img src='style/window_close.png' /></div>";
		
		if ($pre){
			$picPAL = $pre."_".$folderId;
			echo "<a onclick=\"displayTargetPic('$pre','$folderId')\" style='float:left;margin-top:{$marginT}px'><div class='targetArrowLBack' prePicId='$picPAL' ></div></a>";
		}
		if ($next){
			$nextH = "onclick=\"displayTargetPic('$next','$folderId')\"";
		}
		echo "<div style='float:left;'>$closeStr<img id='targetPicDisDo' src='$path' width ='$width' height ='$height' $nextH /><br />$target[name]</div>";
		if($next){
			$picPAR = $next."_".$folderId;
			echo "  <a $nextH style='float:left;margin-top:{$marginT}px'><div class='targetArrowRBack' prePicId='$picPAR'></div></a>";
		}
	}
}else if (!empty($post)&&$post["postType"]==6){
	//delete target picture
	$dao = new picturesdao();
	$parth = $dao->getTargetPicPath($post["picId"]);
	if(file_exists($parth)){
		unlink($parth);
	}
	$deletePic = $dao->deleteTargetPic($post["picId"]);
	if ($deletePic){
		echo "success";
	}else{
		echo "failed";
	}
}else if (!empty($post)&&$post["postType"]==7){
	//set picture folder cover;
	$dao = new picturesdao();
	$path = $dao->getTargetPicPath($post["picId"]);
	$dao->setPictureFolderCover($post["folderId"],$path);
	echo $path;
}else if (!empty($post)&&$post["postType"]==8){
	//set picture folder cover;
	$dao = new picturesdao();
	$pics = $dao->getPicS($post["folderId"]);
	if (!empty($pics)){
		foreach ($pics as $pic){
			if (file_exists($pic["path"])){
				$deletePic = unlink($pic["path"]);
				if (!$deletePic){
					$Log->write("can't delete file:".$pic["path"]);
				}
			}
			$dao->deleteTargetPic($pic["id"]);
		}
	}
	if($dao->deletePicFolder($post["folderId"])){
		echo "folder delete in DB";
	}
	$folder = $post["folderId"];
	$baseFolder = $Helper->getPicturceBaseFolder();
	$dir = $baseFolder.$folder;
	@$deleteDir = rmdir($dir);
	if (!$deleteDir){
		$Log->write("can't delete directory:".$dir);
	}else{
		echo "delete success";
	}
}
?>